package com.itcm.skymail.product.controller;

import com.itcm.common.utils.PageUtils;
import com.itcm.common.utils.R;
import com.itcm.skymail.product.entity.AttrAttrgroupRelationEntity;
import com.itcm.skymail.product.entity.AttrEntity;
import com.itcm.skymail.product.entity.AttrGroupEntity;
import com.itcm.skymail.product.service.AttrGroupService;
import com.itcm.skymail.product.service.CategoryService;
import com.itcm.skymail.product.vo.AttrGroupWithAttrVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author cm_fighting
 * @email cmtianxie@163.com
 * @date 2020-08-27 14:20:36
 */
@Slf4j
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("{catelogId}/withattr")
    public R getGroupsWithAttrByCatId(@PathVariable("catelogId") Long catelogId) {

        List<AttrGroupWithAttrVo> list = attrGroupService.getGroupsWithAttrByCatId(catelogId);

        return R.ok().put("data", list);

    }


    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("attr/relation")
    public R batchSave(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrGroupService.batchSaveRelation(relationEntities);
        return R.ok();
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R getNoRelationAttrList(@RequestParam Map<String, Object> params,
                                   @PathVariable("attrgroupId") Long attrgroupId) {

        PageUtils page = attrGroupService.queryNoRelationPage(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 根据属性id 和 分组id 删除关联关系
     */
    @PostMapping("attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        log.info("delete : {}", relationEntities);
        attrGroupService.deleteRalation(relationEntities);
        return R.ok();
    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R list(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> list = attrGroupService.getRelationListByGroupId(attrgroupId);
        return R.ok().put("data", list);
    }


    /**
     * 根据分类id查询
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> path = categoryService.getCategoryPathById(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody List<Long> attrGroupIds) {
//        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        // 级联删除
        attrGroupService.removeCasByIds(attrGroupIds);

        return R.ok();
    }

}
